package com.frogs.ucenter.v1.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.frogs.framework.component.cache.Cache;
import com.frogs.framework.util.PageList;
import com.frogs.system.util.GoodsEvaluateEnum;
import com.frogs.system.util.UserTypeEnum;
import com.frogs.ucenter.dao.GoodsEvaluateCommentDao;
import com.frogs.ucenter.dao.GoodsEvaluateDao;
import com.frogs.ucenter.dao.UserBaseInfoDao;
import com.frogs.ucenter.dao.UserLevelInfoDao;
import com.frogs.ucenter.entity.GoodsEvaluate;
import com.frogs.ucenter.entity.GoodsEvaluateCommentInfo;
import com.frogs.ucenter.entity.GoodsEvaluateInfo;
import com.frogs.ucenter.entity.UserBaseInfo;
import com.frogs.ucenter.entity.UserLevelInfo;
import com.frogs.ucenter.v1.request.TagGoodsRelationListRequest;

/**
 * 
 *
 * @author lijian
 * @version 2014年3月19日 - 下午7:20:31
 */
public class GoodsEvaluateBaseServiceImpl {
	
	@Resource
	public UserBaseInfoDao userBaseInfoDao;
	@Resource
	public UserLevelInfoDao userLevelInfoDao;
	@Resource
	public GoodsEvaluateDao goodsEvaluateDao;
	
	@Resource
	public GoodsEvaluateCommentDao goodsEvaluateCommentDao;
	
	@Resource(name="xmemcached")
	public Cache cache;

	public int cacheLifeCycle = 60 * 60 * 24 * 5;
	
	/**
	 * 获取商品评价评论列表
	 * 
	 * @param goodsEvaluateId
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public PageList<GoodsEvaluateCommentInfo> getGoodsEvaluateCommentList(Integer goodsEvaluateId, Integer pageNo, Integer pageSize) {
		PageList<GoodsEvaluateCommentInfo> pageList = new PageList<GoodsEvaluateCommentInfo>();
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("eqGoodsEvaluateId", goodsEvaluateId);
		params.put("eqStatus", GoodsEvaluateEnum.AUDITED_PASS.getId());
		PageList<Map<String, Object>> list = goodsEvaluateCommentDao.selectAndUserByMap(params, pageNo, pageSize);
		if (list != null && list.size() > 0) {
			for (int i=0; i<list.size(); i++) {
				Map<String, Object> goodsEvaluateComment = list.get(i);
				GoodsEvaluateCommentInfo info = new GoodsEvaluateCommentInfo();
				info.setId((Integer) goodsEvaluateComment.get("id"));
				info.setApplicablePlatformId((Integer) goodsEvaluateComment.get("applicable_platform_id"));
				info.setContent((String) goodsEvaluateComment.get("content"));
				info.setCreateTime((Date) goodsEvaluateComment.get("create_time"));
				info.setGoodsEvaluateId((Integer) goodsEvaluateComment.get("goods_evaluate_id"));
				info.setGoodsId((Integer) goodsEvaluateComment.get("goods_id"));
				info.setMealId((Integer) goodsEvaluateComment.get("meal_id"));
				info.setOriginalGoodsId((Integer) goodsEvaluateComment.get("original_goods_id"));
				info.setParentCommentId((Integer) goodsEvaluateComment.get("parent_comment_id"));
				info.setParentCommentNickName((String) goodsEvaluateComment.get("parent_comment_nick_name"));
				info.setParentCommentUserId((Integer) goodsEvaluateComment.get("parent_comment_user_id"));
				info.setUserId((Integer) goodsEvaluateComment.get("user_id"));
				info.setUserName((String) goodsEvaluateComment.get("account_name"));
				info.setNickName((String) goodsEvaluateComment.get("nick_name"));
				info.setHeadImageUrl((String) goodsEvaluateComment.get("head_image_url"));
				if (goodsEvaluateComment.get("user_type") != null) {
					info.setUserType((Integer) goodsEvaluateComment.get("user_type"));
					info.setUserTypeName(UserTypeEnum.getByType((Integer) goodsEvaluateComment.get("user_type")).getName());
				} else {
					info.setUserType(UserTypeEnum.GENERAL.getType());
					info.setUserTypeName(UserTypeEnum.getByType(UserTypeEnum.GENERAL.getType()).getName());
				}
			}
			pageList.setPageTurn(list.getPageTurn());
		}
		return pageList;
	}
	
	public GoodsEvaluateInfo setEvaluateInfo(Map<String, Object> object, int isHotEvaluate, int pageNo, int pageSize) {
		GoodsEvaluateInfo goodsEvaluateInfo = new GoodsEvaluateInfo();
		goodsEvaluateInfo.setId((Integer) object.get("id"));
		if(object.get("user_id") != null){
			goodsEvaluateInfo.setUserId((Integer) object.get("user_id"));
			goodsEvaluateInfo.setUserName((String) object.get("account_name"));
		}
		if( object.get("original_goods_id") != null)
			goodsEvaluateInfo.setOriginalGoodsId((Integer) object.get("original_goods_id"));
		goodsEvaluateInfo.setGoodsId((Integer) object.get("goods_id"));
		goodsEvaluateInfo.setMealId((Integer) object.get("meal_id"));
		
		goodsEvaluateInfo.setContent((String) object.get("content"));
		
		if(object.get("is_pharmacist_evaluate") != null)
			goodsEvaluateInfo.setIsPharmacistEvaluate((Integer) object.get("is_pharmacist_evaluate"));
		goodsEvaluateInfo.setReceivedSeveral((Integer) object.get("received_several"));
		goodsEvaluateInfo.setCreateTime((Date) object.get("create_time"));
		goodsEvaluateInfo.setTag((String) object.get("tag"));
		goodsEvaluateInfo.setBuyTime((Date) object.get("buy_time"));
		
		if(object.get("useful_number") != null)
			goodsEvaluateInfo.setUsefulNumber((Integer) object.get("useful_number"));
		
		if(object.get("praise") != null)
			goodsEvaluateInfo.setPraise((Integer) object.get("praise"));
		
		if(object.get("trample") != null)
			goodsEvaluateInfo.setTrample((Integer) object.get("trample"));
		goodsEvaluateInfo.setNickName((String) object.get("nick_name"));
		goodsEvaluateInfo.setHeadImageUrl((String) object.get("head_image_url"));
		if (object.get("user_type") != null) {
			goodsEvaluateInfo.setUserType((Integer) object.get("user_type"));
			if (null == UserTypeEnum.getByType((Integer) object.get("user_type"))) {
				goodsEvaluateInfo.setUserTypeName("");
			} else {
				goodsEvaluateInfo.setUserTypeName(UserTypeEnum.getByType((Integer) object.get("user_type")).getName());
			}
		} else {
			goodsEvaluateInfo.setUserType(UserTypeEnum.GENERAL.getType());
			goodsEvaluateInfo.setUserTypeName(UserTypeEnum.getByType(UserTypeEnum.GENERAL.getType()).getName());
		}
		if (object.get("score") != null) {
			goodsEvaluateInfo.setScore((Integer) object.get("score"));
		}
		else {
			goodsEvaluateInfo.setScore(10);
		}
		goodsEvaluateInfo.setIsHotEvaluate(isHotEvaluate);
		PageList<GoodsEvaluateCommentInfo> list = this.getGoodsEvaluateCommentList((Integer) object.get("id"), pageNo, pageSize);
		goodsEvaluateInfo.setGoodsEvaluateCommentList(list);
		if (list != null && list.size() > 0) {
			goodsEvaluateInfo.setCommentCount(list.size());
		}
		return goodsEvaluateInfo;
	}
	
	/**
	 * 列出有标签的评价列
	 * @return
	 */
	protected List<GoodsEvaluate> getEvaluateListWithTag(TagGoodsRelationListRequest tagRequest) {
		Map<String, Object> params = new HashMap<String, Object>();
		
		if(tagRequest.getGoodsId() > 0)
			params.put("eqGoodsId", tagRequest.getGoodsId());
		if(tagRequest.getMealId() > 0)
			params.put("eqMealId", tagRequest.getMealId());
		
		params.put("eqStatus", GoodsEvaluateEnum.AUDITED_PASS.getId());
		params.put("tagNotNull", GoodsEvaluateEnum.AUDITED_PASS.getId());
		
		List<GoodsEvaluate> list = goodsEvaluateDao.selectByMap(params, tagRequest.getPageNum(), tagRequest.getPageSize());
		return list;
	}
	
	/**
	 * 获取用户名
	 * 
	 * @param userId
	 * @return
	 */
	public String getUserName(Integer userId) {
		UserBaseInfo userBaseInfo = userBaseInfoDao.selectById(userId);
		return userBaseInfo.getNickName();
	}
	
	public UserLevelInfo getUserLevelInfo(Integer userId) {
		UserLevelInfo userLevelInfo = userLevelInfoDao.selectById(userId);
		return userLevelInfo;
	}
}
